package com.ibiaoke.goods.dao;

import com.ibiaoke.common.base.BaseDao;
import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.consts.CommonConsts;
import com.ibiaoke.goods.consts.GoodsConsts;
import com.ibiaoke.goods.entity.GoodsExample;
import com.ibiaoke.goods.entity.PtGoods;
import com.ibiaoke.goods.entity.PtGoodsExample;
import com.ibiaoke.goods.mapper.GoodsMapper;
import com.ibiaoke.goods.mapper.PtGoodsMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class PtGoodsDao extends BaseDao<PtGoods, PtGoodsExample, Integer, PtGoodsMapper> {

    @Autowired
    private PtGoodsMapper ptGoodsMapper;

    @Override
    public PtGoodsMapper getMapper() {
        return ptGoodsMapper;
    }

    /**
     *  保存商品，加入拼团活动
     */
    public int savePtGood(PtGoods PtGoods) {
        return insertSelective(PtGoods);
    }

    public List<PtGoods> listPtGoods(Page page,String special_flag) {
        PtGoodsExample example = new PtGoodsExample();
        if("1".equals(special_flag)){
            //增加指定用户可见商品
            example.createCriteria().andDelStatusEqualTo(CommonConsts.NO).andStatusEqualTo(GoodsConsts.STATUS_ON_SHELF);
        }else{
            example.createCriteria().andDelStatusEqualTo(CommonConsts.NO).andStatusEqualTo(GoodsConsts.STATUS_ON_SHELF).andIsSpecialEqualTo(0);
        }
        page.setTotalCount(ptGoodsMapper.countByExample(example));

        example.setLimitStart(page.limitStart());
        example.setLimitEnd(page.limitEnd());
        example.setOrderByClause("update_time desc");

        return ptGoodsMapper.selectByExample(example);
    }
    /**
     * 根据商品名字，查询商品
     */
    public List<PtGoods> findPtGood(String PtGoodName,Integer ptGoodId,Integer status,Page page){
        PtGoodsExample example = new PtGoodsExample();
        PtGoodsExample.Criteria criteria = example.createCriteria();
        criteria.andDelStatusNotEqualTo(GoodsConsts.STATUS_ON_SHELF);
        if (StringUtils.isNotEmpty(PtGoodName)) {
            criteria.andPtGoodsNameLike("%"+PtGoodName+"%");
        }
        if (ptGoodId != null) {
            criteria.andPtGoodsIdEqualTo(ptGoodId);
        }
        if (status != null) {
            criteria.andStatusEqualTo(status);
        }
        if(page != null){
            page.setTotalCount(ptGoodsMapper.countByExample(example));
            example.setLimitStart(page.limitStart());
            example.setLimitEnd(page.limitEnd());
        }
        return selectByExample(example);
    }

    /**
     * 修改编辑拼团商品
     *
     */
    public  int updatePtGood(PtGoods ptGood){
        return updateByPrimaryKeySelective(ptGood);
    }

    /**
     * 根据商品id，删除商品
     */
    public int  updatePtGood(String PtGoodId){
        PtGoods ptGood =  this.selectByPrimaryKey(Integer.valueOf(PtGoodId));
        ptGood.setDelStatus(GoodsConsts.STATUS_ON_SHELF);
        return updateByPrimaryKey(ptGood);
    }

    //更改ptgood的sale_num数量
    public Integer updatePtGoodSaleNum(Integer goodsId){
            PtGoods ptgood = selectByPrimaryKey(goodsId);
            ptgood.setSaleNum(ptgood.getSaleNum()+1);
            return updateByPrimaryKeySelective(ptgood);
    }

    //更改ptgood的pt_count数量
    public Integer updatePtGoodsPtCount(PtGoods ptGoods,Integer ptCount){
        ptGoods.setPtCount(ptCount);
        return updateByPrimaryKeySelective(ptGoods);
    }
}
